import os
import sys
import stat
import StatStruct
from MyTypeBase import MyTypeBase
from PathDataStorage import PathDataStorage

class MyTypeTemp(MyTypeBase):
	"""MyTypeTemp gives temporary/memory fuse-functionality."""

	def __init__(self, data={}):
		"""Constructor, initializes the temp-type-fuse-object."""
		MyTypeBase.__init__(self)

		self.__data=PathDataStorage(data)

		# Set actually used Blocksize
		statvfs=os.statvfs(".")
		self.__statFsBlockSize=statvfs.f_bsize
	# End def

	def getStatFsBlockSize(self):
		"""Return the actually block size used by the filesystem."""

		return self.__statFsBlockSize
	# End def

	def getStatStruct(self, path):
		st=StatStruct.StatStruct()

		if path==os.sep or self.__data.has_key(path):
			pathItem=self.__data[path]

			if path==os.sep or pathItem.typeOfData() in (pathItem.TYPE_OF_DIR, pathItem.TYPE_OF_LIST):
				st.st_mode=stat.S_IFDIR | 0770
				st.st_nlink=1
				st.st_size=self.getStatFsBlockSize()
				return st
			elif pathItem.typeOfData()==pathItem.TYPE_OF_FILE:
				st.st_mode=stat.S_IFREG | 0770
				st.st_nlink=1

				r=pathItem.getData()
				try:
					st.st_size=len(r)
				except:
					st.st_size=len(str(r))
				# End try/except
			else:
				st=None
			# End if
		else:
			st=None
		# End if

		return st
	# End def


	def hasPath(self, path):
		return self.__data.has_key(path)
	# End def

	def getPathEntries(self, path):
		return self.__data[path].keys()
	# End def

	def getSizeOf(self, path):
		size=sys.getsizeof(str(self.__data[path]))

		return size
	# End def

	def getLengthOf(self, path):
		return len(self.__data[path])
	# End def

	def delData(self, path):
		del self.__data[path]
		return True
	# End def

	def getData(self, path):
		if path==None:
			return self.__data.getData()
		else:
			return self.__data[path].getData()
		# End if
	# End def

	def setData(self, path, value):
		if path==None:
			self.__data.setData(value)
		else:
			self.__data[path]=value
		# End def
		return True
	# End def

# End class
